package com.anzia.study_3.slice;


import com.anzia.study_3.ResourceTable;
import com.anzia.study_3.connections.MyRemoteProxy;
import com.anzia.study_3.utils.DeviceUtils;
import com.anzia.study_3.utils.UIUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
import ohos.agp.render.opengl.Utils;
import ohos.bundle.ElementName;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.IRemoteObject;

public class MainAbilitySlice extends AbilitySlice {

    static final HiLogLabel LOG_LABEL = new HiLogLabel(HiLog.LOG_APP, 0x11102, "MainAbility");

    private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7;

    // 链接PA的远端代理实现
    private MyRemoteProxy myProxy = null;
    private IAbilityConnection conn = new IAbilityConnection() {
        // 链接成功执行该回调
        @Override
        public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int i) {
            myProxy = new MyRemoteProxy(iRemoteObject);
            UIUtils.showTip(MainAbilitySlice.this, myProxy.toString());
        }

        // 这是意外断开的时候才会使用这个回调
        @Override
        public void onAbilityDisconnectDone(ElementName elementName, int i) {
            UIUtils.showTip(MainAbilitySlice.this, "断开链接");
        }
    };

    @Override
    public void onStart(Intent intent) {
        HiLog.info(LOG_LABEL, "============onStart1");
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // LocalService的链接
        btn1 = (Button) findComponentById(ResourceTable.Id_service_btn_01);
        btn1.setClickedListener(component -> {
            Intent intent1 = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("com.anzia.study_3")
                    .withAbilityName(".LocalServiceAbility")
                    .build();
            intent1.setOperation(operation);
            startAbility(intent1);
        });

        // LocalService的断开
        btn2 = (Button) findComponentById(ResourceTable.Id_service_btn_02);
        btn2.setClickedListener(component -> {
            Intent intent2 = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName("com.anzia.study_3")
                    .withAbilityName(".LocalServiceAbility")
                    .build();
            intent2.setOperation(operation);
            stopAbility(intent2);
        });

        // RemoteService的启动
        btn3 = (Button) findComponentById(ResourceTable.Id_service_btn_03);
        btn3.setClickedListener(component -> {
            Intent intent3 = new Intent();
            String deviceId = DeviceUtils.getDeviceId();
            if (deviceId != null) {
                ElementName elementName = new ElementName(deviceId, "com.anzia.study_3", "RemoteServiceAbility");
                intent3.setElement(elementName);
                intent3.setFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE);    // 分布式能力的flag不要忘记
                startAbility(intent3);
            }
        });

        // RemoteService的断开
        btn4 = (Button) findComponentById(ResourceTable.Id_service_btn_04);
        btn4.setClickedListener(component -> {
            Intent intent4 = new Intent();
            String deviceId = DeviceUtils.getDeviceId();
            if (deviceId != null) {
            	// 跳转到Service Ability，以实现里面的服务
                ElementName elementName = new ElementName(deviceId, "com.anzia.study_3", "RemoteServiceAbility");
                intent4.setElement(elementName);
                intent4.setFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE);    // 分布式能力的flag不要忘记
                stopAbility(intent4);
            }
        });

        // RemoteService的链接
        btn5 = (Button) findComponentById(ResourceTable.Id_service_btn_05);
        btn5.setClickedListener(component -> {
            String deviceId = DeviceUtils.getDeviceId();
            if (deviceId != null) {
                Intent intent5 = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId(deviceId)
                        .withBundleName("com.anzia.study_3")
                        .withAbilityName("com.anzia.study_3.RemoteServiceAbility")
                        .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
                        .build();
                intent5.setOperation(operation);
                connectAbility(intent5, conn);
            }
        });

        // 实现Service里的业务
        btn6 = (Button) findComponentById(ResourceTable.Id_service_btn_06);
        btn6.setClickedListener(component -> {
            if (myProxy != null) {
                int rs = myProxy.plus(10, 10);
                UIUtils.showTip(MainAbilitySlice.this, "rs" + rs);
            }
        });

        // RemoteService的断开
        btn7 = (Button) findComponentById(ResourceTable.Id_service_btn_07);
        btn7.setClickedListener(component -> {
            disconnectAbility(conn);
        });

    }

    @Override
    protected void onActive() {
        HiLog.info(LOG_LABEL, "============onActive1");

        super.onActive();
    }

    @Override
    protected void onInactive() {
        HiLog.info(LOG_LABEL, "============onInactive1");

        super.onInactive();
    }

    @Override
    protected void onForeground(Intent intent) {
        HiLog.info(LOG_LABEL, "============onForeground1");

        super.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        HiLog.info(LOG_LABEL, "============onBackground1");

        super.onBackground();
    }

    @Override
    protected void onStop() {
        HiLog.info(LOG_LABEL, "============onStop1");

        super.onStop();
    }
}
